package org.example.spring.repository.service.impl;

import org.example.spring.repository.service.IDepartmentService;
import org.example.spring.repository.service.IManyThreadsTransactionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * LuoYu
 * 2022/12/19 16:25
 **/
@Service
public class ManualTransactionServiceImpl implements IManyThreadsTransactionService {

    private static final int THREAD_SIZE = 3;

    private static final ExecutorService executor = Executors.newFixedThreadPool(THREAD_SIZE);

    @Resource
    private IDepartmentService departmentService;

    @Override
    public void exec() {
        CountDownLatch count = new CountDownLatch(THREAD_SIZE);
        AtomicBoolean single  = new AtomicBoolean(false);
        for (int i = 1; i <= THREAD_SIZE; i++) {
            boolean hasThrow = false;
            executor.execute(() -> departmentService.manualSyncTransactionCreate(UUID.randomUUID().toString().replace("-","").substring(0,10),hasThrow,count,single));
        }
        // 等待线程结束
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
